package exercise4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    ArrayList<Integer> list;
    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true) {
            synchronized (MyCallable.class) {
                if (list.size() == 0) {
                    System.out.println(Thread.currentThread().getName() + boxList);
                    break;
                } else {
                    //继续抽奖
                    Collections.shuffle(list); //打乱集合中的元素
                    Integer price = list.remove(0);   //remove方法会去除某个元素并将去除的元素返回
                    boxList.add(price);
                }
            }
            Thread.sleep(10);
        }
        //把集合中的最大值返回   写在循环外面  因为最大值必须等循环结束后才有
        if (boxList.size()==0){
            return null;
        }else {
            return Collections.max(boxList);
        }
    }
}
